﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace VSM
{
    public partial class WhsPopup : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                || Session[Utils.AppConstants.Password] == null
                || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("Login.aspx");
            }
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            LoadWhs();
            Timer1.Enabled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvWhs_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                DataTable dtWhs = Session["WHSTable"] as DataTable;
                if (dtWhs != null)
                {
                    dtWhs.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
                    Session["WHSTable"] = dtWhs.DefaultView.ToTable();
                    this.grvWhs.DataSource = dtWhs.DefaultView.ToTable();
                    this.grvWhs.DataBind();
                }
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        private string GetSortDirection(string column)
        {
            string sortDirection = "ASC";
            string sortExpression = ViewState["SortExpression"] as string;

            if (sortExpression != null)
            {
                if (sortExpression == column)
                {
                    string lastDirection = ViewState["SortDirection"] as string;
                    if ((lastDirection != null) && (lastDirection == "ASC"))
                    {
                        sortDirection = "DESC";
                    }
                }
            }

            ViewState["SortDirection"] = sortDirection;
            ViewState["SortExpression"] = column;

            return sortDirection;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvWhs_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                GridViewRow row = this.grvWhs.SelectedRow;
                DataTable dt = (DataTable)Session["WHSTable"];
                if (dt != null)
                {
                    int index = row.DataItemIndex;
                    var data = dt.Rows[index];
                    if (data != null)
                    {
                        Session["ChosenCode"] = data["WhsCode"];
                        Session["ChosenName"] = data["WhsName"];
                        //ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('SelectBP');", true);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvWhs_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                    e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)"); 

                    e.Row.Attributes["style"] = "cursor:pointer";
                    e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grvWhs, "Select$" + e.Row.RowIndex);
                }
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable tblWhs = (DataTable)Session["WHSTable"];
                if (tblWhs != null && this.txtSearch.Text.Trim().Length > 0)
                {
                    DataView dv = tblWhs.DefaultView;

                    dv.RowFilter = "WhsCode like '%" + this.txtSearch.Text.Trim() + "%' Or WhsName like '%" + this.txtSearch.Text.Trim() + "%'";

                    this.grvWhs.DataSource = dv.ToTable();
                    this.grvWhs.DataBind();
                }
            }
            catch (Exception ex)
            { 
            
            }
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow selectedRow = this.grvWhs.SelectedRow;
                if (selectedRow == null) return;
                /*Dictionary<string, string> dicBP = new Dictionary<string, string>();
                Label code = (Label)selectedRow.FindControl("lblCardCode");
                Label Name = (Label)selectedRow.FindControl("lblCardName");
                dicBP.Add(code.Text, Name.Text);*/

                /*Dictionary<string, string> dicBP = new Dictionary<string, string>();
                for (int i = 0; i < this.grdBP.Rows.Count; i++)
                {
                    CheckBox checkBox = (CheckBox)this.grdBP.Rows[i].FindControl("chkChild");

                    if (checkBox != null)
                    {
                        if (checkBox.Checked)
                        {
                            Label code = (Label)this.grdBP.Rows[i].FindControl("lblCardCode");
                            Label Name = (Label)this.grdBP.Rows[i].FindControl("lblCardName");
                            dicBP.Add(code.Text, Name.Text);
                        }
                    }
                }*/
                Session["ChosenWhs"] = selectedRow;
                if(Request.QueryString["id"] != null & Request.QueryString["id"].ToString().Equals("to"))
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('SelectWhsTo');", true);
                else if(Request.QueryString["id"] != null & Request.QueryString["id"].ToString().Equals("from"))
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('SelectWhsFrom');", true);
                else ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('SelectWhs');", true);
            }
            catch (Exception ex)
            {

            }
        }

        private void LoadWhs()
        {
            try
            {
                MasterData obj = new MasterData();
                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;

                DataSet dsWhs = obj.GetWarehouse(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());

                if (dsWhs != null && dsWhs.Tables != null & dsWhs.Tables[0].Rows.Count > 0)
                {
                    BindBP(dsWhs.Tables[0]);
                }
            }
            catch (Exception ex)
            { 
            
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindBP(DataTable tb)
        {
            Session["WHSTable"] = tb;
            this.grvWhs.DataSource = tb;
            this.grvWhs.DataBind();
        }
    }
}